package com.nny.alarm.edit;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.nny.alarm.MainActivity;
import com.nny.alarm.R;
import com.nny.alarm.widget.DialView;
import com.nny.alarm.widget.EditLayout;
import com.nny.alarm.widget.TimeView;
import com.umeng.analytics.MobclickAgent;
import com.uxwine.alarm.Alarm;
import com.uxwine.cmm.BaseFragment;
import com.uxwine.util.UxToast;

public class EditFragment extends BaseFragment implements EditLayout.EditHolder, LabelEditDialog.OnActionListener{
	
	EditLayout mMainView;
	MainActivity mAct;
	TimeView mDigital;
	DialView mDial;
	AlarmEditor mEditor;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		mAct = (MainActivity)getActivity();	
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mMainView = (EditLayout)inflater.inflate(R.layout.fragment_edit, container, false);
		mDigital = (TimeView)mMainView.findViewById(R.id.frag_edit_digital);
		mDial = (DialView)mMainView.findViewById(R.id.frag_edit_dial);
		
		mEditor = new AlarmEditor(mAct);
		mEditor.init(mDigital, mDial);
		
		mMainView.setEditHolder(this);
				
		mHandler.sendEmptyMessage(1);
		return mMainView;
	}	
	
	
	
	public void edit(Alarm a)
	{
		mEditor.eidtor(a);
		mMainView.lock(false);
	}
	
	public void hide()
	{
		mMainView.lock(true);
	}
	
	public void stop()
	{
		mEditor.over();
	}

	@Override
	public void onLockedClick(EditLayout v)
	{
		mMainView.lock(false);
		mAct.showEidtor(null);
	}

	@Override
	public void onEditFling(EditLayout v, int type)
	{
		mEditor.over();
		
		if(type == EditLayout.FLING_DOWN)
		{
			mEditor.reset();
			mEditor.eidtor(null);
			UxToast.sshow(getActivity(), R.string.edit_alarm_reset);
			MobclickAgent.onEvent(getActivity(), "editor_fling", "down");
		}
		else if(type == EditLayout.FLING_UP)
		{
			LabelEditDialog dlg = new LabelEditDialog(mAct);
			dlg.setEditor(mEditor);	
			dlg.setOnActionListener(this);
			dlg.show();
			MobclickAgent.onEvent(getActivity(), "editor_fling", "up");
		}
		else if(type == EditLayout.FLING_LEFT)
		{
			//UxToast.sshow(getActivity(), "Alarm Model Changed!!");
		}
		else if(type == EditLayout.FLING_RIGHT)
		{
			mAct.showMore();	
			MobclickAgent.onEvent(getActivity(), "editor_fling", "right");		
		}
	}	

	@Override
	public void onExit(int code)
	{
		if(code == LabelEditDialog.ACTION_OK)
		{
			mEditor.save();
			mAct.showMore();
		}		
	}
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 1)
			{
				mEditor.eidtor(null);
			}
	}};
}
